<?php

namespace app\service;

use app\models\Inquiry;
use app\models\Customer;

class CustomerService
{
    /**
     * 添加询盘同时添加以分配客户到客户表
     * @param array $array
     * @return bool
     */
    public function insert($array)
    {
        $model = new Customer();
        //获取当前最大客户编号
        $num = $model->getMaxNumV2();
        $num++;
        //insert into customer
        $customerArr = [];
        foreach ($array as $v) {
            if (end($v) == 2) {
                $customerArr[] = $v;
            }
        }


        foreach ($customerArr as $v) {
            $re = $model->searchByEmail($v[9]);
            if (!$re) {
                $insert_arr = [];
                $insert_arr['number'] = 'K' . date('ymd') . str_pad($num, 3, 0, STR_PAD_LEFT);
                $insert_arr['customer_name'] = $v[1];
                $insert_arr['email'] = $v[9];
                $insert_arr['country'] = $v[8];
                $insert_arr['follow_man'] = "[\"".$v[13]."\"]";
                $insert_arr['customer_star'] = $v[12];
                $insert_arr['refer'] = $v[10];
                $model->insert($insert_arr);
                $num++;
            }

        }
        $model = new Inquiry();
        return $model->insert_inquiry($array);
    }
}